package com.example.hanfu.collect;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.example.hanfu.firstpage.HanFu;

import java.util.ArrayList;
import java.util.List;

public class CollectDBmanager {
    private CollectDBHelper dbHelper;
    //private String TBNAME1;
    //private String TBNAME2;
    private static final String TAG = "CollectDBmanager";
    public CollectDBmanager(Context context) {
        dbHelper = new CollectDBHelper(context);
        //TBNAME1 = CollectDBHelper.TB_NAME1;
        //TBNAME2 = CollectDBHelper.TB_NAME2;
    }



    public void deleteAll(String TBNAME){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(TBNAME,null,null);
        db.close();
    }

    public void delete(String id,String TBNAME){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete(TBNAME, "id=?", new String[]{String.valueOf(id)});
        db.close();
    }

    public void add(Collect item, String TBNAME){
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("username", item.getUserName());
        values.put("title",item.getHanFu().getTitle());
        Log.i(TAG,"title为："+item.getHanFu().getTitle());
        values.put("href",item.getHanFu().getHref());
        values.put("content",item.getHanFu().getContent());
        values.put("img",item.getHanFu().getImg());
        db.insert(TBNAME, null, values);
        db.close();
    }

/*
    public Collect findById(String TBNAME,String userName){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, "username=?", new String[]{userName}, null, null, null);
        Collect item = null;
        if(cursor!=null && cursor.moveToFirst()){
            item = new Collect();
            item.setUserName(cursor.getString(cursor.getColumnIndex("username")));
            HanFu hanfu=null;
            hanfu.setTitle(cursor.getString(cursor.getColumnIndex("title")));
            hanfu.setContent(cursor.getString(cursor.getColumnIndex("content")));
            hanfu.setHref(cursor.getString(cursor.getColumnIndex("href")));
            hanfu.setImg(cursor.getString(cursor.getColumnIndex("img")));
            item.setHanFu(hanfu);
            cursor.close();
        }
        db.close();
        return item;
    }

 */


    public List<Collect> listAll(String TBNAME,String userName){
        List<Collect> list = null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(TBNAME, null, "username=?", new String[]{userName}, null, null, null);
        if(cursor!=null){
            list = new ArrayList<Collect>();
            while(cursor.moveToNext()){
                Collect item = new Collect();
                item.setUserName(cursor.getString(cursor.getColumnIndex("username")));
                HanFu hanfu=new HanFu();
                hanfu.setContent(cursor.getString(cursor.getColumnIndex("content")));
                hanfu.setHref(cursor.getString(cursor.getColumnIndex("href")));
                hanfu.setImg(cursor.getString(cursor.getColumnIndex("img")));
                hanfu.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                item.setHanFu(hanfu);
                list.add(item);
            }
            cursor.close();
        }
        db.close();
        return list;
    }
}
